<template>
  <div class="layout">
    <div class="table-layout">
      <div class="table-header">
        <el-row>
          <el-form ref="queryForm" :inline="true" :model="queryParams" class="demo-form-inline">
            <el-form-item label="字典名称" prop="label">
              <el-input v-model="queryParams.label" placeholder="请输入" clearable />
            </el-form-item>
            <el-form-item label="字典编码" prop="code">
              <el-input v-model="queryParams.code" placeholder="请输入" clearable />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择" clearable>
                <el-option
                  v-for="v in statusList"
                  :key="v.value"
                  :label="v.label"
                  :value="v.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="table-body">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          height="100%"
          stripe
          row-key="id"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column align="left" prop="label" label="字典名称" min-width="160" />
          <el-table-column align="center" prop="code" label="字典编码" />
          <el-table-column align="center" prop="status" label="状态">
            <template slot-scope="{row}">
              <el-tag effect="dark" :type="['success','info'][row.status]">{{ ['启用','停用'][row.status] }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="dictSort" label="序号" width="80" />
          <el-table-column align="center" prop="createTime" label="创建时间" min-width="140" />
          <el-table-column align="center" label="操作">
            <template slot-scope="{row}">
              <el-link v-hasPermi="['system:treeDict:add']" title="新增" type="primary" @click="handleAdd(row)">
                <i class="el-icon-circle-plus-outline" />
              </el-link>
              <span v-hasPermi="['system:treeDict:edit']" class="ml-5 mr-5">|</span>
              <el-link v-hasPermi="['system:treeDict:edit']" title="编辑" type="success" @click="handleEdit(row)">
                <i class="el-icon-edit-outline" />
              </el-link>
              <span v-hasPermi="['system:treeDict:remove']" class="ml-5 mr-5">|</span>
              <el-link v-hasPermi="['system:treeDict:remove']" title="删除" type="danger" @click="handleDelete(row)">
                <i class="el-icon-delete" />
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="table-footer">
        <pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page.sync="pageParams.pageNum"
          :limit.sync="pageParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <add-form ref="addForm" :menu-options="[{ id: 0, label: '主类目', children: tableData }]" @resetList="searchHandle" />
  </div>
</template>

<script>
import { getPage, del } from '@/api/system/treeDict'
import addForm from './form.vue'
import page_common from '@/views/mixins/page_common.js'
export default {
  name: 'TreeDict',
  components: { addForm },
  mixins: [page_common],
  data() {
    return {
      queryParams: {
        label: '',
        status: '',
        code: ''
      },
      tableData: [],
      statusList: [
        { value: '0', label: '启用' },
        { value: '1', label: '停用' }
      ]
    }
  },
  mounted() {
    this.searchHandle()
  },
  methods: {
    getList() {
      this.loading = true
      getPage({ ...this.queryParams, ...this.pageParams }).then(res => {
        const { rows, total } = res
        this.tableData = rows
        this.total = total
        this.loading = false
      })
    },
    // 新增
    handleAdd(rowData) {
      this.$refs.addForm.reset('add', rowData.id)
    },
    handleEdit(rowData) {
      this.$refs.addForm.reset('edit', rowData.id)
    },
    async del(rowData) {
      await del({ id: rowData.id })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
